var app = getApp();
var Bmob = require('../../utils/bmob.js');
var util = require('../../utils/util.js');

var pageData = {
  data: {

    imageList: [],
    swiperInfo: [],
    goods_data: [],
    height: 0,
    imgWidth: 0,
    loadingCount: 10,
  top:0,
    hidden:true
  },

  onLoad: function (e) {
    // app.onPageLoad(e);
    this.dataInitial();

  },
  scroll: function (event) {
    //  该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。  
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  }, 
  onImageLoad: function (e) {

    var that = this;
    that.setData({
      hidden: false
    });
    var Image = Bmob.Object.extend("Phone_image");
    var image = new Bmob.Query(Image);
    var loadingCount = that.data.loadingCount;
    var imageList=that.data.imageList;
    
    image.limit(10);
    image.skip(loadingCount);
    // featureinfo.include("goods_Info")
    image.find({
      success: function (res) {
        console.log(res);
        imageList=imageList.concat(res);
     
        that.setData({
          imageList: imageList,
          loadingCount: loadingCount+10,
          hidden: true
        })
      }

    })

    },
 

  getheight: function () {
    var that = this;

    wx.getSystemInfo({
      success: function (res) {

        that.setData({
          height: res.windowHeight,
          img_height: res.screenWidth/3*2436/1370,
          img_width: res.screenWidth / 3-5,
        })
      }
    })
  },
  dataInitial: function () {
    // app.pageDataInitial();
    this.get_swiperInfo();
   this.get_icon();
    this.get_image();
  
    this.get_goods();
    this.getheight();
  },

  get_icon: function () {
    var that = this;
    //var album5 = that.data.album5
    var Icon = Bmob.Object.extend("page_Icon");
    var icon = new Bmob.Query(Icon);
    icon.equalTo("state", true);
    icon.equalTo("pageid", 4);
    icon.ascending("sepuence");
    icon.find({
      success: function (res) {
        //album5.li=res;
        console.log(res);
        that.setData({
          IconInfo: res
        })

      }
    })
  },
  get_image: function () {
    var that = this;
    var Image = Bmob.Object.extend("Phone_image");
    var image = new Bmob.Query(Image);

    image.limit(10);
    // featureinfo.include("goods_Info")
    image.find({
      success: function (res) {
        console.log(res);
        that.setData({
          imageList: res
        })
      }

    })

  },
  previewImage:function(e){
       
var index=e.currentTarget.dataset.index;
var image=this.data.imageList[index].get("image");
var imagelist=[];

imagelist.push(image);
    wx.previewImage({
      current: image, // 当前显示图片的http链接
      urls: imagelist
    })
  
  },
  turnToSeckillDetail: function (e) {
    console.log(e);
    app.turnToSeckillDetail(e);
  },
  get_swiperInfo: function () {
    var that = this;
    var Swiper = Bmob.Object.extend("swiper");
    var swiper = new Bmob.Query(Swiper);
    var list = [];
    swiper.equalTo("page_Id", '4');
    swiper.find({
      success: function (res) {
        console.log(res);

        that.setData({
          swiperInfo: res
        })
      }

    })


  },

  turntopage:function(e){
 
    var id=e.currentTarget.dataset.id;
    var links = e.currentTarget.dataset.links;
    var url = links + "?id=" + id;

    wx.navigateTo({
      url: url
    });

  },
  turnToGood: function (){
  
  var url = "../../pages/goodsMore/goodsMore?franchisee=VaM0bRRT&type=0";
  
  wx.navigateTo({
    url: url
  });

},
  get_goods: function (skip) {
    var that = this;
    var Goods = Bmob.Object.extend("goods");
    var goods = new Bmob.Query(Goods);
    var Franchisee = Bmob.Object.extend("Franchisee");
    var franchisee = new Franchisee();
    franchisee.id = "VaM0bRRT";
    goods.equalTo("FranchiseeId", franchisee);
    goods.notEqualTo("state", 0);
    goods.limit(3);
    goods.descending("hot");
    goods.skip(skip);
    goods.find({
      success: function (res) {
        //var goods=res;
       
        that.setData({
          goods_data: res,

        })
      },
      complete: function () {

      }
    });
  },
  turnToArtical: function (e) {
    console.log(e);
    var url = e.currentTarget.dataset.link + "?objectId=" + e.currentTarget.dataset.id
    wx.navigateTo({
      url: url
    });
  },
  fresh:function(){
    var that=this;
    var Goods = Bmob.Object.extend("goods");
    var goods = new Bmob.Query(Goods);
    var Franchisee = Bmob.Object.extend("Franchisee");
    var franchisee = new Franchisee();
    franchisee.id = "VaM0bRRT";
    goods.equalTo("FranchiseeId", franchisee);
    goods.count({
      success: function (count) {
        // 查询成功，返回记录数量
        var skip = Math.round(Math.random() * (count - 3))
        if(skip<0)
        skip=0;
        that.get_goods(skip);
        console.log("共有 " + count + " 条记录");
      },
      error: function (error) {
        // 查询失败
      }
    });



  },
  onShareAppMessage: function (res) {


    return {
      title: '邻舍边手机小铺',
      path: '/pages/Phoneshop/Phoneshop'
    }
  },
  onShow: function () {
 //   app.onPageShow();
  },

  scrollPageTop: function () {
 var that=this;
    that.setData({
      top:0
    })
  
  },
  tapPhoneCallHandler: function (e) {
    app.tapPhoneCallHandler(e);
  },

  suspensionTurnToPage: function (e) {
    app.suspensionTurnToPage(e);
  },
};
Page(pageData);
